package com.qbw.app.module.common.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.view.Gravity;
import android.view.View;

import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.CommonFragmentOkCancelBinding;
import com.qbw.core.base.BaseDialogFragment;

/**
 * @author QBW
 * @date 2019-09-22
 */
public class OkCancelDialogFragment extends BaseDialogFragment<CommonFragmentOkCancelBinding> implements View.OnClickListener {

    private int mType;
    private String mTitle;
    private int mGravity;

    public static OkCancelDialogFragment createInstance(int type, @StringRes int title) {
        return createInstance(type, title, Gravity.CENTER);
    }

    public static OkCancelDialogFragment createInstance(int type,
                                                        @StringRes int title,
                                                        int gravity) {
        return createInstance(type,
                              XApplication.getInstance().getResources().getString(title),
                              gravity);
    }

    public static OkCancelDialogFragment createInstance(int type, String title) {
        return createInstance(type, title, Gravity.CENTER);
    }

    public static OkCancelDialogFragment createInstance(int type, String title, int gravity) {
        Bundle args = new Bundle();
        args.putInt(Constant.KEY_TYPE, type);
        args.putString(Constant.KEY_TITLE, title);
        args.putInt(Constant.KEY_GRAVITY, gravity);
        OkCancelDialogFragment fragment = new OkCancelDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_ok_cancel;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mType = args.getInt(Constant.KEY_TYPE);
        mTitle = args.getString(Constant.KEY_TITLE);
        mGravity = args.getInt(Constant.KEY_GRAVITY);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.tvTitle.setGravity(mGravity);
        mBinding.tvTitle.setText(mTitle);
        mBinding.tvSure.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(Constant.KEY_TYPE, mType);
        outState.putString(Constant.KEY_TITLE, mTitle);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_sure) {
            dismiss();
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickSure(mType);
            }
        } else if (vid == R.id.tv_cancel) {
            dismiss();
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickCancel(mType);
            }
        }
    }

    private Listener getListener() {
        Fragment fragment = getParentFragment();
        if (fragment instanceof Listener) {
            return (Listener) fragment;
        }
        FragmentActivity activity = getActivity();
        if (activity instanceof Listener) {
            return (Listener) activity;
        }
        return null;
    }

    public interface Listener {
        void onClickSure(int type);

        void onClickCancel(int type);
    }
}
